﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace MediaCenter
{
    public partial class StartUp : Form
    {
        private string[] pathFilmer;

        private GetSerier.GetSerier serier = new GetSerier.GetSerier();
        private GetMusic.GetMusic musikk = new GetMusic.GetMusic();

        public StartUp()
        {
            InitializeComponent();
            fillUpMovies();
            fillUpSerier();
            fillUpMusic();
            Console.WriteLine(listBoxSeries.Items.Count + " " + listBoxSeries.SelectedIndex);
        }

        public void fillUpSerier()
        {
            foreach (string f in serier.getDir())
            {
                //listBox2.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
                listBoxSeries.Items.Add(f);
            }

        }

        public void fillUpMusic()
        {
            foreach (string m in musikk.getDir())
            {
                listBoxMusic.Items.Add(m);
            }
        }

        public void fillUpMovies()
        {
            string[] paths = {"P:\\Filmer","J:\\filmer-1"};
            GetMovies.GetMovies movies = new GetMovies.GetMovies(paths);

            foreach (string f in movies.getFileNames())
            {
                listBoxMovies.Items.Add(f);
            }

            pathFilmer = movies.getFullPaths();
        }

        private void btn_Spotify_Click(object sender, EventArgs e)
        {
            new StartSpotify().lauchSpotify();
        }

        private void btn_AmbiLight_Click(object sender, EventArgs e)
        {
            new UseLed().Show();
        }

        private void StartUp_Load(object sender, EventArgs e)
        {

        }

        private void listBoxSeries_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SelectedIndex = listBoxSeries.SelectedIndex;

            string[] temp = new string[listBoxSeries.Items.Count];
            listBoxSeries.Items.CopyTo(temp, 0);
            serier.setWinList(temp);

            listBoxSeries.Items.Clear();

            if (SelectedIndex == -1)
                SelectedIndex = 0;
            try{
                foreach (string f in serier.getSubDir(temp[SelectedIndex]))
                    listBoxSeries.Items.Add(f);

                foreach (string f in serier.getEpisoder(temp[SelectedIndex]))
                    listBoxSeries.Items.Add(f);
            }
            catch (NullReferenceException e1){
                new StartMovie().watchMovie(temp[SelectedIndex]);
                listBoxSeries.Items.Clear();
                foreach (string f in serier.getDir())
                {
                   // listBox2.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
                  listBoxSeries.Items.Add(f);
                }
            }
        }

        private void listBoxMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            new StartMovie().watchMovie(pathFilmer[listBoxMovies.SelectedIndex]);
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            if (menuTabControl.SelectedTab.Equals("Serier")) {
                listBoxSeries.Items.Clear();
                foreach (string f in serier.getDir())
                {
                    //listBox2.Items.Add(f.Substring(f.LastIndexOf('\\') + 1));
                    listBoxSeries.Items.Add(f);
                }
            }
            if (menuTabControl.SelectedTab.Equals("Musikk")) {
                listBoxMusic.Items.Clear();
                foreach (string m in musikk.getDir())
                {
                    listBoxMusic.Items.Add(m);
                }
            }
        }

        private void listBoxMusic_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SelectedIndex = listBoxMusic.SelectedIndex;

            string[] temp = new string[listBoxMusic.Items.Count];
            listBoxMusic.Items.CopyTo(temp, 0);
            musikk.setWinList(temp);

            listBoxMusic.Items.Clear();

            if (SelectedIndex == -1)
                SelectedIndex = 0;
            try
            {
                foreach (string f in musikk.getSubDir(temp[SelectedIndex]))
                    listBoxMusic.Items.Add(f);

                foreach (string f in musikk.getSanger(temp[SelectedIndex]))
                    listBoxMusic.Items.Add(f);
            }
            catch (NullReferenceException e1)
            {
                new StartMovie().watchMovie(temp[SelectedIndex]);
                listBoxMusic.Items.Clear();
                foreach (string f in musikk.getDir())
                {
                    listBoxSeries.Items.Add(f);
                }
            }
        }

    }
}
